package com.gaga.lession04;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaga.lession04.mapper.SysUserMapper;
import com.gaga.lession04.pojo.SysUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SysUserMapperTester {
    @Resource
    private SysUserMapper sysUserMapper;

    //主键查询
    @Test
    public void testSelectById(){
        SysUser sysUser=sysUserMapper.selectById(2L);
        System.out.println("userName:"+sysUser.getUsrName());
    }

    //添加
    @Test
    public void testInsert(){
        sysUserMapper.insert(new SysUser("MyBatis-Plus","123456",1L,1));
    }

    //查询所有
    @Test
    public void testFindAll(){
        List<SysUser> list=sysUserMapper.selectList(null);
        for (SysUser sysUser : list) {
            System.out.println("userName:"+sysUser.getUsrName());
        }
    }

    //条件查询：根据用户名和密码查询
    @Test
    public void testFind(){
        QueryWrapper<SysUser> wrapper=new QueryWrapper<SysUser>();
        wrapper.eq("usr_name","MyBatis-Plus");
        wrapper.eq("usr_password","123456");
        List<SysUser> list=sysUserMapper.selectList(wrapper);
        if (list!=null){
            for (SysUser sysUser : list) {
                System.out.println("usrId:"+sysUser.getUsrId());
            }
        }
    }

    //分页查询
    @Test
    public void testPagination(){
        QueryWrapper<SysUser> wrapper=new QueryWrapper<SysUser>();
        wrapper.eq("usr_role_id",2L);  //查询条件
        //分页对象：查询获得SysUser对象，查看第1页数据，每页显示2条数据
        Page<SysUser> page=new Page<SysUser>(1,2);
        IPage<SysUser> userPage=sysUserMapper.selectPage(page,wrapper);
        System.out.println("总记录数 ---- -->"+userPage.getTotal());
        System.out.println("总页数 ---- ---->"+userPage.getPages());
        System.out.println("当前页数 ---- -->"+userPage.getCurrent());
        System.out.println("每页记录数 -- -->"+userPage.getSize());
        System.out.println("当前记录: ---- -->");
        //获取查询记录
        for (SysUser user : userPage.getRecords()) {
            System.out.println("usrId:"+user.getUsrId());
        }
    }
}
